package com.chinasoft.mall.activity.subActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Observable;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.BaseObserverActivity;
import com.chinasoft.mall.activity.adapter.JmdListAdapter;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.framework.LivingGoods;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.EpgBean;
import com.chinasoft.mall.util.DateUtil;
import com.chinasoft.mall.widget.LoadListView;

/**
 * 节目单
 * @author Administrator
 *
 */
public class ProgramListActivity extends BaseObserverActivity implements
		OnClickListener {

	private BaseBean erp_bb;
	private BaseBean erp_pre_bb;
	private BaseBean erp_next_bb;
	private LoadListView modellist;
	private JmdListAdapter mlAdapter;
	private LinearLayout left_btn;
	private LinearLayout center_btn;
	private LinearLayout right_btn;
	private TextView left_date;
	private TextView left_day;
	private TextView center_date;
	private TextView center_day;
	private TextView right_date;
	private TextView right_day;
	private ImageButton jmd_left;
	private ImageButton jmd_right;

	// 0 昨天 1 今天 2 明天
	public byte jdmState = 1;
	public HashMap<Byte, Integer> jdmChoice = new HashMap<Byte, Integer>(
			3);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jmd_list);
		initData();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	private void initData() {
		jdmChoice.clear();
		jdmChoice.put((byte)0, -1);
		jdmChoice.put((byte)1, -1);
		jdmChoice.put((byte)2, -1);
		String _track = getIntent().getStringExtra(Config.INTENT_TRACK);
		if (_track != null) {
			jdmState = Byte.parseByte(_track);
		}
	}

	private void initRequest() {
		switch (jdmState) {
		case 0:
			erp_pre_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG_PRE);
			if (erp_pre_bb != null) {
				success(erp_pre_bb);
				erp_pre_bb = new BaseBean(Config.GET_EPG_PRE);
				control.postMessage(erp_pre_bb);
			} else {
				erp_pre_bb = new BaseBean(Config.GET_EPG_PRE);
				control.sendMessage(erp_pre_bb);
			}
			break;
		case 1:
			erp_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG);
			if (erp_bb != null) {
				success(erp_bb);
//				erp_bb = new BaseBean(Config.GET_EPG);
//				control.postMessage(erp_bb);
			} else {
				erp_bb = new BaseBean(Config.GET_EPG);
				control.sendMessage(erp_bb);
			}
			break;
		case 2:
			erp_next_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG_NEXT);
			if (erp_next_bb != null) {
				success(erp_next_bb);
				erp_next_bb = new BaseBean(Config.GET_EPG_NEXT);
				control.postMessage(erp_next_bb);
			} else {
				erp_next_bb = new BaseBean(Config.GET_EPG_NEXT);
				control.sendMessage(erp_next_bb);
			}
			break;
		default:
			break;
		}
	}

	private void initView() {
		jmd_left = (ImageButton) findViewById(R.id.jmd_left);
		jmd_right = (ImageButton) findViewById(R.id.jmd_right);
		modellist = (LoadListView) findViewById(R.id.modellist);
		left_btn = (LinearLayout) findViewById(R.id.left_btn);
		left_date = (TextView) findViewById(R.id.left_date);
		left_day = (TextView) findViewById(R.id.left_day);
		center_btn = (LinearLayout) findViewById(R.id.center_btn);
		center_date = (TextView) findViewById(R.id.center_date);
		center_day = (TextView) findViewById(R.id.center_day);
		right_btn = (LinearLayout) findViewById(R.id.right_btn);
		right_date = (TextView) findViewById(R.id.right_date);
		right_day = (TextView) findViewById(R.id.right_day);
		mlAdapter = new JmdListAdapter(this, null);
		modellist.setAdapter(mlAdapter);
		
		left_btn.setOnClickListener(this);
		center_btn.setOnClickListener(this);
		right_btn.setOnClickListener(this);
		jmd_left.setOnClickListener(this);
		jmd_right.setOnClickListener(this);

		String yesterday = DateUtil.yesterdayDate();
		String now = DateUtil.nowDate();
		String tomorrow = DateUtil.tomorrowDate();
		left_date.setText(DateUtil.formatDate(yesterday));
		left_day.setText(DateUtil.getWeekOfDate(yesterday));
		center_date.setText(DateUtil.formatDate(now));
		center_day.setText(DateUtil.getWeekOfDate(now));
		right_date.setText(DateUtil.formatDate(tomorrow));
		right_day.setText(DateUtil.getWeekOfDate(tomorrow));
		
		modellist.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				jdmChoice.put(jdmState,modellist.getFirstVisiblePosition());
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if (obj.equals(erp_bb)) {
			jdmState = 1;
			center_btn.setBackgroundResource(R.drawable.jmd_btn_bg);
			left_btn.setBackgroundColor(0x00ffffff);
			right_btn.setBackgroundColor(0x00ffffff);
			List<EpgBean> cur_ebs = LivingGoods.getInstance().getEpgBeans();
//			List<EpgBean> cur_ebs = (List<EpgBean>)obj.getResData().getData();
			if (cur_ebs != null && !cur_ebs.equals(mlAdapter.getData())) {
				mlAdapter.setData(cur_ebs);
				modellist.setSelection(choice(cur_ebs));
			}
			if(jdmChoice.get(jdmState)!=-1){
				modellist.setSelection(jdmChoice.get(jdmState));
			}
		} else if (obj.equals(erp_pre_bb)) {
			jdmState = 0;
			left_btn.setBackgroundResource(R.drawable.jmd_btn_bg);
			center_btn.setBackgroundColor(0x00ffffff);
			right_btn.setBackgroundColor(0x00ffffff);
			List<EpgBean> cur_ebs = (List<EpgBean>) obj.getResData().getData();
			if (cur_ebs != null && !cur_ebs.equals(mlAdapter.getData())) {
				mlAdapter.setData(cur_ebs);
			}
			if(jdmChoice.get(jdmState)!=-1){
				modellist.setSelection(jdmChoice.get(jdmState));
			}
		} else if (obj.equals(erp_next_bb)) {
			jdmState = 2;
			right_btn.setBackgroundResource(R.drawable.jmd_btn_bg);
			center_btn.setBackgroundColor(0x00ffffff);
			left_btn.setBackgroundColor(0x00ffffff);
			List<EpgBean> cur_ebs = (List<EpgBean>) obj.getResData().getData();
			if (cur_ebs != null && !cur_ebs.equals(mlAdapter.getData())) {
				mlAdapter.setData(cur_ebs);
			}
			if(jdmChoice.get(jdmState)!=-1){
				modellist.setSelection(jdmChoice.get(jdmState));
			}
		}
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.left_btn:
			erp_pre_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG_PRE);
			if (erp_pre_bb != null) {
				success(erp_pre_bb);
				erp_pre_bb = new BaseBean(Config.GET_EPG_PRE);
				control.postMessage(erp_pre_bb);
			} else {
				erp_pre_bb = new BaseBean(Config.GET_EPG_PRE);
				control.sendMessage(erp_pre_bb);
			}
			break;
		case R.id.center_btn:
			erp_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG);
			if (erp_bb != null) {
				success(erp_bb);
				erp_bb = new BaseBean(Config.GET_EPG);
				control.postMessage(erp_bb);
			} else {
				erp_bb = new BaseBean(Config.GET_EPG);
				control.sendMessage(erp_bb);
			}
			break;
		case R.id.right_btn:
			erp_next_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG_NEXT);
			if (erp_next_bb != null) {
				success(erp_next_bb);
				erp_next_bb = new BaseBean(Config.GET_EPG_NEXT);
				control.postMessage(erp_next_bb);
			} else {
				erp_next_bb = new BaseBean(Config.GET_EPG_NEXT);
				control.sendMessage(erp_next_bb);
			}
			break;
		case R.id.jmd_left:
			if (jdmState == 1) {
				erp_pre_bb = BeanCache.getInstance().getResponsdata()
						.get(Config.GET_EPG_PRE);
				if (erp_pre_bb != null) {
					success(erp_pre_bb);
					erp_pre_bb = new BaseBean(Config.GET_EPG_PRE);
					control.postMessage(erp_pre_bb);
				} else {
					erp_pre_bb = new BaseBean(Config.GET_EPG_PRE);
					control.sendMessage(erp_pre_bb);
				}
			} else if (jdmState == 2) {
				erp_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG);
				if (erp_bb != null) {
					success(erp_bb);
					erp_bb = new BaseBean(Config.GET_EPG);
					control.postMessage(erp_bb);
				} else {
					erp_bb = new BaseBean(Config.GET_EPG);
					control.sendMessage(erp_bb);
				}
			}
			break;
		case R.id.jmd_right:
			if (jdmState == 1) {
				erp_next_bb = BeanCache.getInstance().getResponsdata()
						.get(Config.GET_EPG_NEXT);
				if (erp_next_bb != null) {
					success(erp_next_bb);
					erp_next_bb = new BaseBean(Config.GET_EPG_NEXT);
					control.postMessage(erp_next_bb);
				} else {
					erp_next_bb = new BaseBean(Config.GET_EPG_NEXT);
					control.sendMessage(erp_next_bb);
				}
			} else if (jdmState == 0) {
				erp_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_EPG);
				if (erp_bb != null) {
					success(erp_bb);
					erp_bb = new BaseBean(Config.GET_EPG);
					control.postMessage(erp_bb);
				} else {
					erp_bb = new BaseBean(Config.GET_EPG);
					control.sendMessage(erp_bb);
				}
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void updateActivity(Observable observable, Object data) {
		int refreshtype = (Integer) data;
		if (refreshtype == LivingGoods.Refresh_PLAY && jdmState == 1) {
			List<EpgBean> cur_ebs = LivingGoods.getInstance().getEpgBeans();
			if (cur_ebs != null && !cur_ebs.equals(mlAdapter.getData())) {
				mlAdapter.setData(cur_ebs);
				modellist.setSelection(choice(cur_ebs));
				int k=modellist.getSelectedItemPosition();
				System.out.println(k);
			}
		}
	}

	public int choice(List<EpgBean> cur_ebs) {
		for (int i = 0; i < cur_ebs.size(); i++) {
			if (cur_ebs.get(i).getLivingState() == 0) {
				return i;
			}
		}
		for (int i = 0; i < cur_ebs.size(); i++) {
			if (cur_ebs.get(i).getLivingState() != 1) {
				return 0;
			}
		}
		return cur_ebs.size();
	}
}
